package com.xhcl.web.controller.system;


import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xhcl.common.annotation.Log;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.common.utils.StringUtils;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.framework.shiro.util.ShiroUtils;
import com.xhcl.system.domain.TSrhsBkpzti;
import com.xhcl.system.domain.TSrhsBkpzto;
import com.xhcl.system.domain.TSrhsZfpzto;
import com.xhcl.system.service.ITSrhsBkpztiService;
import com.xhcl.system.service.ITSrhsBkpztoService;

/**
 * 拨款凭证头Controller
 * 
 * @author xhcl
 * @date 2020-09-12
 */
@Controller
@RequestMapping("/system/bkpzto")
public class TSrhsBkpztoController extends BaseController
{
    private String prefix = "system/bkpzto";

    @Autowired
    private ITSrhsBkpztoService tSrhsBkpztoService;
    
    @Autowired
    private ITSrhsBkpztiService tSrhsBkpztiService;

    @RequiresPermissions("system:bkpzto:view")
    @GetMapping()
    public String bkpzto()
    {
        return prefix + "/bkpzto";
    }

    /**
     * 查询拨款凭证头列表
     */
    @RequiresPermissions("system:bkpzto:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSrhsBkpzto tSrhsBkpzto)
    {
        startPage();
        List<TSrhsBkpzto> list = tSrhsBkpztoService.selectTSrhsBkpztoList(tSrhsBkpzto);
        return getDataTable(list);
    }

    /**
     * 导出拨款凭证头列表
     */
    @RequiresPermissions("system:bkpzto:export")
    @Log(title = "拨款凭证头", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSrhsBkpzto tSrhsBkpzto)
    {
        List<TSrhsBkpzto> list = tSrhsBkpztoService.selectTSrhsBkpztoList(tSrhsBkpzto);
        ExcelUtil<TSrhsBkpzto> util = new ExcelUtil<TSrhsBkpzto>(TSrhsBkpzto.class);
        return util.exportExcel(list, "bkpzto");
    }

    /**
     * 新增拨款凭证头
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存拨款凭证头
     */
    @RequiresPermissions("system:bkpzto:add")
    @Log(title = "拨款凭证头", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TSrhsBkpzto tSrhsBkpzto)
    {   
    	String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(tSrhsBkpztoService.insertTSrhsBkpzto(tSrhsBkpzto,operName));
    }

    /**
     * 修改拨款凭证头
     */
    @GetMapping("/edit/{bktxh}")
    public String edit(@PathVariable("bktxh") Long bktxh, ModelMap mmap)
    {   
    	
        TSrhsBkpzto tSrhsBkpzto = tSrhsBkpztoService.selectTSrhsBkpztoById(bktxh);
        TSrhsBkpzti tSrhsBkpzti=new TSrhsBkpzti();
        tSrhsBkpzti.setBktxh(bktxh);
        List<TSrhsBkpzti> list=tSrhsBkpztiService.selectTSrhsBkpztiList(tSrhsBkpzti);
        if (tSrhsBkpzto!=null &&(tSrhsBkpzto.getBklx().equals(1L)||tSrhsBkpzto.getBklx().equals(2L))){
        	for (TSrhsBkpzti pzti: list){
        	  if (StringUtils.isEmpty(pzti.getYskm())){
        		  pzti.setYskm(pzti.getJfkmdm());
        		  pzti.setYskmmc(pzti.getJfkmmc());
        	  }
        	  
        	}
        }
        mmap.put("tSrhsBkpzto", tSrhsBkpzto);
        mmap.put("tSrhsBkpztiList", list);
        return prefix + "/edit";
    }

    /**
     * 修改保存拨款凭证头
     */
    @RequiresPermissions("system:bkpzto:edit")
    @Log(title = "拨款凭证头", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSrhsBkpzto tSrhsBkpzto)
    {   
    	String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(tSrhsBkpztoService.updateTSrhsBkpzto(tSrhsBkpzto,operName));
    }

    /**
     * 删除拨款凭证头
     */
    @RequiresPermissions("system:bkpzto:remove")
    @Log(title = "拨款凭证头", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tSrhsBkpztoService.deleteTSrhsBkpztoByIds(ids));
    }
    
    @RequiresPermissions("system:bkpzto:verify")
    @Log(title = "审核凭证", businessType = BusinessType.DELETE)
    @PostMapping( "/verify")
    @ResponseBody
    public AjaxResult verify(String ids)
    {   String operName = ShiroUtils.getSysUser().getLoginName();
    tSrhsBkpztoService.verifyTSrhsBkpztoById(ids, operName);
        return AjaxResult.success();
    }
    
    @RequiresPermissions("system:bkpzto:cancel")
    @Log(title = "撤销拨款凭证", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(TSrhsBkpzto tSrhsBkpzto)
    {   
    	tSrhsBkpztoService.BkpzCancel(tSrhsBkpzto);
        return AjaxResult.success();
    }
}
